package com.groep14.project;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.groep14.manager.SpecificatieManager;
import com.groep14.model.ActiveOffer;
import com.groep14.model.Person;

/**
 * De HomeController handelt requests met de homepagina af.
 * 
 * @since r45 - 9 April 2012
 * @author Maarten Tutak
 *
 */

@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	private MyLogger myLogger = new MyLogger();
	
	/**
	 * getModelAndView word gebruikt om huidige offers te laten zien op de homepagina.
	 * @param session
	 * @return ModelAndView
	 */
	private ModelAndView getModelAndView (HttpSession session) {
		ModelAndView modelAndView = new ModelAndView("home");
		ArrayList<ActiveOffer> activeoffers = new ArrayList<ActiveOffer>();
		ResultSet result;
		
		try {
			Connectie con = new Connectie();
			
			con.setpStatement(con.getCon().prepareStatement("SELECT o.offer_id, p.EAN_UCC_13, p.Name, p.Description, p.Price, o.AvailablilityEnd, o.MinParticipators, o.Discount,SUM(x.Aantal) 'aantal' FROM Offers o JOIN Products p ON o.EAN_UCC_13 = p.EAN_UCC_13 LEFT OUTER JOIN persons_in_offer x ON o.Offer_id = x.Offer_id WHERE o.AvailablilityStarts <= NOW() AND o.AvailablilityEnd >= NOW() GROUP BY o.offer_id,p.EAN_UCC_13,p.Name,p.Description,p.Price, o.AvailablilityEnd,o.MinParticipators,o.Discount"));
			result = con.getpStatement().executeQuery();
			
			while(result.next()) {
				ActiveOffer o = new ActiveOffer();
				
				o.setOffer_id(result.getInt(1)); //toegevoegd
				o.setEAN_UCC_13(result.getInt(2));
				o.setName(result.getString(3));
				o.setDescription(result.getString(4));
				o.setPrice(result.getFloat(5));
				o.setAvailabilityEnd(result.getDate(6));
				o.setMinParticipators(result.getInt(7));
				o.setDiscount(result.getFloat(8));
				o.setAmount(result.getInt(9));
				
				activeoffers.add(o);					
			}
			
			if(activeoffers.size() != 0) {
				modelAndView.addObject("activeoffers", activeoffers);	
				logger.info(String.valueOf(activeoffers.size()));
			} else {
				//geen aanbiedingen op dit moment?
			}
			
			Person p = (Person) session.getAttribute("person");       
		    if(p != null) {
		    	modelAndView.addObject("firstname", p.getFirstname());
		    	modelAndView.addObject("admin", p.getAdmin());
		    }
			
		} catch (SQLException e) {
			myLogger.logException(getClass() + " - ", e);
		} catch (ClassNotFoundException e) {
			myLogger.logException(getClass() + " - ", e);
		} catch (InstantiationException e) {
			myLogger.logException(getClass() + " - ", e);
		} catch (IllegalAccessException e) {
			myLogger.logException(getClass() + " - ", e);
		}
				
		return modelAndView;  
		
		
	}
	
	/**
	 * De home methode 
	 * @param session
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home(HttpSession session) {
        return getModelAndView(session);
	}
	
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public ModelAndView index(HttpSession session) {
		return getModelAndView(session);
	}
	
	/**
	 * Specificaties van een product laten zien, dat product word kenbaar gemaakt door de parameter ID.
	 * @param ID
	 * @param session
	 * @return ModelAndView
	 */
	@RequestMapping(value = "specs",method = RequestMethod.GET)
	public ModelAndView specs(@RequestParam(value="ID",required=false) int ID, HttpSession session) {
		
		SpecificatieManager s = new SpecificatieManager(ID);
		
		ModelAndView MaV = s.getModelAndView();
		
		Person p = (Person) session.getAttribute("person");
        if(p != null) {
	        MaV.addObject("firstname", p.getFirstname());
	        MaV.addObject("admin", p.getAdmin());
        }
		
		return MaV;	
	}
	
}
